package com.yeban.javase.day08.test03;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/16
 * <p>
 * 一个简易的银行取款操作，定义一个类ExceptionTest，
 * 其中有String id 和double的balance两个属性，以及withdraw()取款方法。
 * 通过withdraw()中的方法判断取出的钱，是否小于等于银行的余额。
 * 通过main方法调用 withdraw()类 ，传入取出的钱，
 * 并且用try-catch捕获异常并且处理，并且在finally块，显示余额。
 * 需要的关键字（try-catch-finally throw throws）
 */
public class ExceptionTest {

    public static void main(String[] args) throws BalanceNotEnoughException1 {

        Account acc = new Account("001",1000);
        try{
            //acc.withdraw(800);
            acc.withdraw(1500);
        } catch (BalanceNotEnoughException1 e) {
            e.printStackTrace();
        } finally {
            System.out.println("当前余额为：" + acc.getBalance());
        }

    }

}

class Account {
    private String id;
    private double balance;

    public Account() {
    }

    public Account(String id, double balance) {
        this.id = id;
        this.balance = balance;
    }

    public void withdraw(double money) throws BalanceNotEnoughException1 {
        if (this.balance >= money) {
            this.balance -= money;
        }else {
            throw new BalanceNotEnoughException1("账户余额不足");  // 编译期异常
        }
    }

    /**
     * 获取
     *
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     *
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取
     *
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * 设置
     *
     * @param balance
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    public String toString() {
        return "Account{id = " + id + ", balance = " + balance + "}";
    }
}
